package com.suzj.extraenchantments.item;

import com.suzj.extraenchantments.ExtraEnchantments;
import com.suzj.extraenchantments.enchant.EnchantRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.EnchantedBookItem;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentInstance;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;



public class ModCreativeTabs {
    public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, ExtraEnchantments.MODID);
    public static final RegistryObject<CreativeModeTab> EXAMPLE_TAB = CREATIVE_MODE_TABS.register("example_tab", () -> CreativeModeTab.builder()
            .withTabsBefore(CreativeModeTabs.COMBAT)
            .icon(() -> ItemRegistry.wutongguo.get().getDefaultInstance())
            .title(Component.translatable("itemGroup.extraenchantments"))
            .displayItems((parameters, output) -> {
                output.accept(ItemRegistry.wutongguo.get());

                List<RegistryObject<Enchantment>> result = new ArrayList<>();
                // 获取 EnchantRegistry 类的所有字段
                Field[] fields = EnchantRegistry.class.getDeclaredFields();
                for (Field field : fields) {
                    // 检查字段类型是否为 RegistryObject<Enchantment>
                    if (RegistryObject.class.isAssignableFrom(field.getType())) {
                        // 检查字段是否为 public static
                        int modifiers = field.getModifiers();
                        if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
                            try {
                                // 获取字段值并添加到结果列表
                                RegistryObject<?> registryObject = (RegistryObject<?>) field.get(null);
                                if (registryObject.get() instanceof Enchantment enchantment) {
                                    if (enchantment.isAllowedOnBooks()  ) {
                                        output.accept(EnchantedBookItem.createForEnchantment(new EnchantmentInstance(enchantment, enchantment.getMaxLevel())));
                                    }
                                }
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }

            }).build());
}
